
#include "helper.h"
su8 helper_easeInEaseOutGenerate(Helper_EaseInEaseOut_Context *context)
{
    su8 err = 0;
    if (context->key != context->target)
    {
        context->key = (context->key * 7 + context->target) >> 3;
        if (context->key >= context->target)
        {
            err = context->key - context->target;
        }
        else
        {
            err = context->target - context->key;
        }
        if (err <= 8)
        {
            context->key = context->target;
        }
    }
    return context->key;
}

su8 helper_easeGenerate(Helper_Ease_Context *context)
{
    su8 err = 0;
    if (context->key != context->target)
    {
        context->key = (context->key *  context->slop + context->target) /(context->slop+1);
        if (context->key >= context->target)
        {
            err = context->key - context->target;
        }
        else
        {
            err = context->target - context->key;
        }
        if (err <= (context->slop+1))
        {
            context->key = context->target;
        }
    }
    return context->key;
}

void helper_wheel(su8 pos, Color *color)
{
    pos = 255 - pos;
    if (pos < 85)
    {
        color->R = 255 - pos - pos - pos;
        color->G = 0;
        color->B = pos + pos + pos;
    }
    else if (pos < 170)
    {
        pos -= 85;
        color->R = 0;
        color->G = pos + pos + pos;
        color->B = 255 - pos - pos - pos;
    }
    else
    {
        pos = pos - 170;
        color->R = pos + pos + pos;
        color->G = 255 - pos - pos - pos;
        color->B = 0;
    }
}

//计算A和B的差值之间斜率直接值
su8 helper_colorMix(su8 A, su8 B, su8 ABTotal, su8 ABIndex)
{
    su8 ret = 0;
    su8 ERR = 0;
    if (A > B)
    {
        ERR = (A - B); //scale 100X

        ERR = ERR * ABIndex / ABTotal;
        ret = A - ERR;
    }
    else if (B > A)
    {
        ERR = (B - A); //scale 100X
        ERR = ERR * ABIndex / ABTotal;
        ret = A + ERR;
    }
    else
    {
        ERR = 0;
        ret = A;
    }
    //index 0~255 map to ERR A~B
    return (su8)ret;
}